import {createSlice,PayloadAction,createAsyncThunk} from '@reduxjs/toolkit'
import axios from 'axios'
interface PaymentState{
    isloading:boolean,
    error:string|null,
    orderlist:any[]
}
const initialState:PaymentState={
    isloading:false,
    error:null,
    orderlist:[]
}
export const getPayment=createAsyncThunk(
    "Payment/getpayment",
    async (params:{token:string,orderid:string|number})=>{
        const {data} = await axios.get('http://123.56.149.216:8080/api/orders/'+params.orderid,{
            headers:{
                "Authorization":"bearer "+params.token,
                "x-icode": "69ACF3816BF5D32D"
            }
        })
        return data.orderItems;
    }
)
export const PaymentSlice=createSlice({
    name:'Payment',
    initialState,
    reducers:{},
    extraReducers:{
        [getPayment.pending.type]:(state)=>{
            state.isloading=true;
        },
        [getPayment.fulfilled.type]:(state,action)=>{
            state.isloading=false;
            state.orderlist=action.payload;
        },
        [getPayment.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        },

        // [checkout.fulfilled.type]:(state)=>{
        //     state.isloading=true;
        // },
        // [checkout.fulfilled.type]:(state)=>{
        //     state.isloading=false;
        //     state.buycarproducts=[];
        // },
        // [checkout.rejected.type]:(state,action)=>{
        //     state.isloading=false;
        //     state.error=action.error.message;
        // },
    }
})